package com.example.recyclerview;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.domain.Girls;
import com.example.myapplication.R;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/5/17.
 */
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyHolder> {
    private final Context context;
    private final List<Girls.ResultsBean> girls;
    private ArrayList<Integer> heights;

    public RecyclerViewAdapter(Context context, List<Girls.ResultsBean> girls) {
        this.context = context;
        this.girls = girls;
    }
    private void getRandomHeight(List<Girls.ResultsBean> lists){//得到随机item的高度
        heights = new ArrayList<>();
        for (int i = 0; i < lists.size(); i++) {
            heights.add((int)(400+Math.random()*400));
        }
    }
    public interface RecyclerViewOnItemClickLitener{
        void onItemClick(View view,int position);
        void onItemLongClick(View view,int position);
    }
    private RecyclerViewOnItemClickLitener recyclerViewOnItemClickLitener;
    public void setRecyclerViewOnItemClickLitener(RecyclerViewOnItemClickLitener recyclerViewOnItemClickLitener){
        this.recyclerViewOnItemClickLitener = recyclerViewOnItemClickLitener;
    }

    @Override
    public RecyclerViewAdapter.MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerViewAdapter.MyHolder holder = new MyHolder(
                LayoutInflater.from(context).inflate(R.layout.item_1,parent,false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final RecyclerViewAdapter.MyHolder holder, final int position) {
        String url = girls.get(position).getUrl();
        Uri uri = Uri.parse(url);
        getRandomHeight(this.girls);
        ViewGroup.LayoutParams params =  holder.itemView.getLayoutParams();//得到item的LayoutParams布局参数
        params.height = heights.get(position);//把随机的高度赋予itemView布局
        holder.itemView.setLayoutParams(params);//把params设置给itemView布局
        Glide
                .with(context)
                .load(uri)
                .crossFade()
                .into(holder.fresco);
        holder.fresco.setScaleType(ImageView.ScaleType.CENTER_CROP);
        if(recyclerViewOnItemClickLitener != null){
            holder.fresco.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    recyclerViewOnItemClickLitener.onItemClick(holder.fresco,position);
                }
            });
            holder.fresco.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    recyclerViewOnItemClickLitener.onItemLongClick(holder.fresco,position);
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return girls.size();
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        private SimpleDraweeView fresco;
        public MyHolder(View itemView) {
            super(itemView);
            fresco = (SimpleDraweeView)itemView.findViewById(R.id.item_fresco);
        }
    }
}
